#!/bin/bash
function init() {
    DEVKIT_PATH="$(pwd)"
    WORKSPACE="${DEVKIT_PATH}/workspace"
    BUILD_PATH="${WORKSPACE}/cangjie_runtime"
    INSTALL_PATH="usr/lib/x86_64-linux-gnu"
    CANGJIE_PATH="/opt/cangjie"
    RUNTIME_PATH="${CANGJIE_PATH}/runtime/lib/linux_x86_64_llvm"
    DEB_NAME="cangjie-runtime-${cjversion}.deb"
    RESULT_PATH="${DEVKIT_PATH}/target"
    mkdir -p "${RESULT_PATH}"

    if [ -d "${WORKSPACE}" ];
    then
        rm -rf "${WORKSPACE}"
    fi
    mkdir -p "${BUILD_PATH}"
}

function build_cangjie_runtime() {
    DEB_PATH="${BUILD_PATH}/DEBIAN"
    mkdir -p "${DEB_PATH}"
    cat > ${DEB_PATH}/control <<EOF
Package: cangjie-runtime
Version: ${cjversion}
Maintainer: Huawei
Section: utils
Priority: optional
Architecture: amd64
Pre-Depends: debconf
Description: canjie runtime Ver.${cjversion}
EOF
    cd "${BUILD_PATH}"
    chmod 755 ./DEBIAN/*
    mkdir -p "${BUILD_PATH}/${INSTALL_PATH}"
    cp "${RUNTIME_PATH}/"* "${BUILD_PATH}/${INSTALL_PATH}"
    chmod -R 644 "${BUILD_PATH}/${INSTALL_PATH}"
    dpkg-deb -b "." "../${DEB_NAME}"
    mv "../${DEB_NAME}" "${RESULT_PATH}"
}

init
build_cangjie_runtime